package com.superpeer.baseproject.wight;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.superpeer.baseproject.R;

public class LoadingDialog {
    /** 加载数据对话框 */
    private static Dialog mLoadingDialog;
    /**
     * 显示加载对话框
     * @param context 上下文
     * @param msg 对话框显示内容
     * @param cancelable 对话框是否可以取消
     */
    public static Dialog showDialogForLoading(Activity context, String msg, boolean cancelable) {
        if(mLoadingDialog != null){
            if(mLoadingDialog.isShowing()){
                mLoadingDialog.cancel();
            }
        }
        View view = LayoutInflater.from(context).inflate(R.layout.base_dialog_loading, null);
        TextView loadingText = (TextView) view.findViewById(R.id.tvDialogLoading);
        loadingText.setText(msg);
        mLoadingDialog = new Dialog(context, R.style.custom_dialog);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

        if(!context.isFinishing()){
            mLoadingDialog.show();
        }
        return  mLoadingDialog;
    }

    public static Dialog showDialogForLoading(Activity context) {
//        View view = LayoutInflater.from(context).inflate(R.layout.base_dialog_loading, null);
//        TextView loadingText = (TextView)view.findViewById(R.id.tvDialogLoading);
//        loadingText.setText("加载中...");
//
//        mLoadingDialog = new Dialog(context, R.style.CustomProgressDialog);
//        mLoadingDialog.setCancelable(true);
//        mLoadingDialog.setCanceledOnTouchOutside(false);
//        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
//        if(mLoadingDialog != null){
//            if(mLoadingDialog.isShowing()){
//                mLoadingDialog.cancel();
//            }
//        }
//        Log.e("打开loading","111");
//        mLoadingDialog.show();
        return  showDialogForLoading(context,"加载中...",true);
    }

    /**
     * 关闭加载对话框
     */
    public static void cancelDialogForLoading() {
        if(mLoadingDialog != null) {
            mLoadingDialog.cancel();
        }
    }

    public static boolean isShowing(){
        if(mLoadingDialog.isShowing()){
            return true;
        }else{
            return false;
        }
    }
}
